package com.tech.connect.zhaofuwu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.ksy.common.dialog.BaseDialog;
import com.ksy.common.dialog.DateChooseDialog;
import com.ksy.common.dialog.ItemsDialog;
import com.ksy.common.model.City;
import com.tech.connect.R;
import com.tech.connect.activity.BaseActivity;
import com.tech.connect.activity.CityChooseActivity;
import com.tech.connect.activity.EditTextActivity;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.LvXinHttp;
import com.tech.connect.model.ItemCatSub;
import com.tech.connect.util.CatUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class PublishLvYouYaoActivity extends BaseActivity {


    private TextView tvCity;
    private TextView tvWho;
    private TextView tvStartDate;
    private TextView tvBetweenDate;
    private TextView tvHow;
    private TextView tvMoney;
    private TextView tvNote;
    private String money;
    private String how;
    private String between;
    private String start;
    private String who;
    private String cityCode;
    private String note;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getHeadBar().setTitle("旅行邀约").setRightText("发布", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEmpty(cityCode)) {
                    showToast("请选择城市");
                    return;
                }
                if (isEmpty(who)) {
                    showToast("请选择邀约对象");
                    return;
                }
                if (isEmpty(start)) {
                    showToast("请选择出发时间");
                    return;
                }
                if (isEmpty(between)) {
                    showToast("请选择旅行时长");
                    return;
                }
                if (isEmpty(how)) {
                    showToast("请选择旅行方式");
                    return;
                }
                if (isEmpty(money)) {
                    showToast("请选择旅行花费");
                    return;
                }
//                if (isEmpty(note)) {
//                    showToast("请填写旅行安排");
//                    return;
//                }
                update();
            }
        });
        setContentView(R.layout.activity_publish_yaoyue);
        initView();
    }

    private void update() {
        Map<String, Object> map = LvXinHttp.getMap();
        map.put("cityCode", cityCode);
        map.put("description", note);
        map.put("invitations", who);
        map.put("longTime", between);
        map.put("startTime", start);
        map.put("travelFee", money);
        map.put("travelMode", how);
        LvXinHttp.createYao(map, new BaseEntityOb<String>() {
            @Override
            public void onStart() {
                super.onStart();
                showDialog();
            }

            @Override
            public void onDataDeal(boolean isSuccess, String data, String msg) {
                hideDialog();
                showToast(msg);
                if (isSuccess) {
                    setResult(RESULT_OK);
                    destroyActivity();
                }
            }
        });
    }

    private void initView() {
        tvCity = findViewById(R.id.tvCity);
        tvWho = findViewById(R.id.tvWho);
        tvStartDate = findViewById(R.id.tvStartDate);
        tvBetweenDate = findViewById(R.id.tvBetweenDate);
        tvHow = findViewById(R.id.tvHow);
        tvMoney = findViewById(R.id.tvMoney);
        tvNote = findViewById(R.id.tvNote);
        tvCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(CityChooseActivity.class, 10001);
//                AreaChooseDialog areaChooseDialog=new AreaChooseDialog(activity);
//                areaChooseDialog.show();
            }
        });
        tvWho.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final List<ItemCatSub> items = CatUtils.getInstance().getCatListByCode("travel-invite-object");
                ItemsDialog<ItemCatSub> itemsDialog = new ItemsDialog<>(activity, items, new ItemsDialog.ItemChooseListener() {
                    @Override
                    public void choose(ItemsDialog dialog, int position) {
                        ItemCatSub itemCatSub = items.get(position);
                        dialog.dismiss();
                        tvWho.setText(itemCatSub.description);
                        who = itemCatSub.value;

                    }
                });
                itemsDialog.show();
            }
        });
        tvStartDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DateChooseDialog dateChooseDialog = new DateChooseDialog(activity);
                dateChooseDialog.setOnClickListener(new DateChooseDialog.ClickListener() {
                    @Override
                    public void enter(BaseDialog dialog, DateChooseDialog.Year y, DateChooseDialog.Month m, DateChooseDialog.Day d) {
                        dialog.dismiss();
                        start = y + "-" + m + "-" + d;
                        tvStartDate.setText(start);
                    }
                });
                dateChooseDialog.show();
//                final List<ItemCatSub> items = CatUtils.getInstance().getCatListByCode("travel-depart-time");
//                ItemsDialog<ItemCatSub> itemsDialog = new ItemsDialog<>(activity, items, new ItemsDialog.ItemChooseListener() {
//                    @Override
//                    public void choose(ItemsDialog dialog, int position) {
//                        ItemCatSub itemCatSub = items.get(position);
//                        dialog.dismiss();
//                        tvStartDate.setText(itemCatSub.description);
//                        start = itemCatSub.value;
//
//                    }
//                });
//                itemsDialog.show();
            }
        });
        tvBetweenDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final List<ItemCatSub> items = CatUtils.getInstance().getCatListByCode("travel-hour");
                ItemsDialog<ItemCatSub> itemsDialog = new ItemsDialog<>(activity, items, new ItemsDialog.ItemChooseListener() {
                    @Override
                    public void choose(ItemsDialog dialog, int position) {
                        ItemCatSub itemCatSub = items.get(position);
                        dialog.dismiss();
                        tvBetweenDate.setText(itemCatSub.description);
                        between = itemCatSub.value;

                    }
                });
                itemsDialog.show();
            }
        });
        tvHow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final List<ItemCatSub> items = CatUtils.getInstance().getCatListByCode("travel-trip-mode");
                ItemsDialog<ItemCatSub> itemsDialog = new ItemsDialog<>(activity, items, new ItemsDialog.ItemChooseListener() {
                    @Override
                    public void choose(ItemsDialog dialog, int position) {
                        ItemCatSub itemCatSub = items.get(position);
                        dialog.dismiss();
                        tvHow.setText(itemCatSub.description);
                        how = itemCatSub.value;

                    }
                });
                itemsDialog.show();
            }
        });
        tvMoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final List<ItemCatSub> items = CatUtils.getInstance().getCatListByCode("travel-telephone-fare");
                ItemsDialog<ItemCatSub> itemsDialog = new ItemsDialog<>(activity, items, new ItemsDialog.ItemChooseListener() {
                    @Override
                    public void choose(ItemsDialog dialog, int position) {
                        ItemCatSub itemCatSub = items.get(position);
                        dialog.dismiss();
                        tvMoney.setText(itemCatSub.description);
                        money = itemCatSub.value;

                    }
                });
                itemsDialog.show();
            }
        });
        tvNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, EditTextActivity.class);
                intent.putExtra("data", tvNote.getText().toString().trim());
                jump2Activity(intent, 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {
                String data = intent.getStringExtra("data");
                tvNote.setText(data);
                note = data;
            } else if (requestCode == 10001) {
                //城市切换
                Serializable data = intent.getSerializableExtra("data");
                if (data != null && data instanceof City) {
                    City selectCity = (City) data;
                    cityCode = String.valueOf(selectCity.id);
                    tvCity.setText(selectCity.name);
                }
            }
        }
    }
}